package com.xiaoyu.awakening.common.core.dto;

import lombok.Getter;
import lombok.Setter;

import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 分页结果封装类
 *
 * @author wangxiaoyu
 * &#064;date  2024/5/29 15:18
 * @since v1.0
 */
@Setter
@Getter
public class Page<T> implements Serializable {
    @Serial
    private static final long serialVersionUID = -1417748095004687576L;
    private int pageSize;
    private int pageIndex;
    private int firstEntityIndex;
    private int lastEntityIndex;
    private List<T> records;
    private int total;
    private int pageCount;

    public Page(List<T> records, int pageIndex, int pageSize, int total) {
        this.records = records;
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
        this.total = total;
        firstEntityIndex = (pageIndex - 1) * pageSize;
        lastEntityIndex = pageIndex * pageSize;
        if (total % pageSize > 0) {
            pageCount = total / pageSize + 1;
        } else {
            pageCount = total / pageSize;
        }
    }

    public Page(int pageSize, int pageIndex, int total) {
        if (pageIndex > 1 && pageSize <= 0) {
            throw new IllegalArgumentException(
                    "Illegal paging arguments. [pageSize=" + pageSize
                            + ", pageIndex=" + pageIndex + "]");
        }

        if (pageSize < 0) {
            pageSize = 0;
        }
        if (pageSize < 1) {
            pageSize = 1;
        }

        this.records = new ArrayList<>();
        this.pageSize = pageSize;
        this.pageIndex = pageIndex;
        this.total = total;
        firstEntityIndex = (pageIndex - 1) * pageSize;
        lastEntityIndex = pageIndex * pageSize;
        if (total % pageSize > 0) {
            pageCount = total / pageSize + 1;
        } else {
            pageCount = total / pageSize;
        }
    }

    public Page(int pageSize, long firstEntityIndex, int total) {
        if (firstEntityIndex > 1 && pageSize <= 0) {
            throw new IllegalArgumentException(
                    "Illegal paging arguments. [pageSize=" + pageSize
                            + ", firstEntityIndex=" + firstEntityIndex + "]");
        }

        if (pageSize < 0) {
            pageSize = 0;
        }
        this.firstEntityIndex = (int) firstEntityIndex;
        if (firstEntityIndex < 0) {
            this.firstEntityIndex = 0;
        }

        this.pageSize = pageSize;
        this.total = total;
        lastEntityIndex = pageSize + (int) firstEntityIndex;
        if (total % pageSize > 0) {
            pageCount = total / pageSize + 1;
        } else {
            pageCount = total / pageSize;
        }
    }

    /**
     * @param pageSize  每页记录数
     * @param pageIndex 页号
     */
    public Page(int pageSize, int pageIndex) {
        if (pageIndex > 1 && pageSize <= 0) {
            throw new IllegalArgumentException(
                    "Illegal paging arguments. [pageSize=" + pageSize
                            + ", pageIndex=" + pageIndex + "]");
        }

        if (pageSize < 0) {
            pageSize = 0;
        }
        if (pageIndex < 1) {
            pageIndex = 1;
        }

        this.pageSize = pageSize;
        this.pageIndex = pageIndex;
        firstEntityIndex = (pageIndex - 1) * pageSize;
        lastEntityIndex = pageIndex * pageSize;
    }

}
